﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuanLyCayXanh.Models;
using System.Data;

namespace QuanLyCayXanh.Controllers
{
    public class FamilyTreeController : Controller
    {
        private QuanLyCayXanhContext db = new QuanLyCayXanhContext();

        //
        // GET: /FamilyTree/
        public ActionResult Index()
        {
            ViewBag.Content = "Danh sách các dòng họ cây";
            return View(db.FamilyTrees.ToList());
        }
        public ActionResult Create()
        {
            return View();
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(FamilyTree familyTree)
        {
            if (ModelState.IsValid)
            {
                db.FamilyTrees.Add(familyTree);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(familyTree);
        }
        
        [HttpPost, ActionName("Delete")]   
        public ActionResult DeleteConfirmed(int id)
        {
            FamilyTree familyTree = db.FamilyTrees.Find(id);
            db.FamilyTrees.Remove(familyTree);
            db.SaveChanges();
            return Content(Boolean.TrueString);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }


        public ActionResult Edit(int id=0)
        {
            FamilyTree familyTree = db.FamilyTrees.Find(id);
            if (familyTree == null)
                return HttpNotFound();
            return View(familyTree);
        }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(FamilyTree familyTree)
        {
            if (ModelState.IsValid)
            {
                db.Entry(familyTree).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(familyTree);
        }

        //
        // GET: /Tree/Details/5
        public ActionResult Details(int id = 0)
        {
            FamilyTree familyTree = db.FamilyTrees.Find(id);
            if (familyTree == null)
                return HttpNotFound();
            return PartialView(familyTree);
        }        

    }
}
